﻿/* Copyright 2010 Igor Francé
 * 
 * This file is part of ECMADoc (http://ecmadoc.codeplex.com).
 * 
 * ECMADoc is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * ECMADoc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with ECMADoc. 
 * If not, see http://www.gnu.org/licenses/.
 */
namespace EcmaDoc.Gui.Controls
{
	using System;
	using System.IO;
	using System.Windows.Forms;

	public partial class ProjectFileEntryEditor : UserControl
	{
		public event EventHandler Change;
		private bool isDirectoryEntry;

		public ProjectFileEntryEditor()
		{
			InitializeComponent();

			this.GotFocus += OnProjectFileEntryEditorGotFocus;
			this.PathControl.KeyDown += OnPathControlKeyDown;
		}

		public void Show(string initialPath, bool isDirectory)
		{
			this.PathControl.Text = initialPath;
			this.isDirectoryEntry = isDirectory;
			this.Visible = true;
		}

		public string FilePath
		{
			get
			{
				return this.PathControl.Text;
			}
			set
			{
				bool fireChanged = this.PathControl.Text != value;
				this.PathControl.Text = value;
				if (fireChanged)
					FireOnChange();
			}
		}

		private void BrowseFilePath()
		{
			if (this.isDirectoryEntry)
				BrowseDirectory();
			else
				BrowseFile();
		}

		private void OnPathControlKeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				FireOnChange();
				Hide();
			}
			if (e.KeyData == Keys.Escape)
			{
				Hide();
			}
		}

		private void OnProjectFileEntryEditorGotFocus(object sender, EventArgs e)
		{
			this.Visible = true;
			this.PathControl.Focus();
			this.PathControl.SelectAll();
		}

		private void BrowseDirectory()
		{
			FileFolderBrowseDialog dialog = new FileFolderBrowseDialog();
			dialog.Description = "Select the path to this folder";
			dialog.ShowNewFolderButton = true;
			dialog.ShowEditBox = true;
			dialog.ShowFullPathInEditBox = true;
			dialog.ShowBothFilesAndFolders = true;
			dialog.SelectedPath = this.PathControl.Text;
			dialog.RootFolder = System.Environment.SpecialFolder.MyComputer;

			DialogResult result = dialog.ShowDialog();
			if (result == DialogResult.OK)
			{
				this.FilePath = dialog.SelectedPath;
			}
		}

		private void BrowseFile()
		{
			OpenFileDialog dialog = new OpenFileDialog();
			if (!string.IsNullOrEmpty(this.FilePath))
				dialog.InitialDirectory = Path.GetDirectoryName(this.FilePath);

			dialog.Filter = "Javascript files (*.js)|*.js|All files (*.*)|*.*";
			dialog.Title = "Select a source file";

			DialogResult result = dialog.ShowDialog();
			if (result == DialogResult.OK)
			{
				this.FilePath = dialog.FileName;
			}
		}

		private void FireOnChange()
		{
			if (Change != null)
				Change(this, EventArgs.Empty);
		}

		private void OnSelectPathButtonClick(object sender, EventArgs e)
		{
			BrowseFilePath();
		}
	}
}
